﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GtsServer.Service.Model;
using GtsServer.DAL;
using System.Data;
using GtsServer.Common;
using GtsServer.Model;

namespace GtsServer.Service.Controller
{
    public class MatchController
    {
        public MatchListResponse GetMatchList(MatchListRequest request)
        {
            MatchListResponse response = new MatchListResponse()
            {
                match = new List<MatchListManage>()
            };
            string userAccount = BaseController.Account(request.info);

            if (string.IsNullOrEmpty(userAccount))
                return response;

            TaskDAL dal = new TaskDAL();
            DataSet ds = dal.GetMatchList(userAccount, Convert.ToInt32(EnumTaskType.比赛));

            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow item in ds.Tables[0].Rows)
                {
                    MatchListManage model = new MatchListManage();

                    model.id = Convert.ToInt32(item["id"].ToString());
                    model.name = item["name"].ToString();
                    DateTime dt = DateTime.Parse(item["start"].ToString());
                    string minute = dt.Minute.ToString().Length == 1 ? "0" + dt.Minute : dt.Minute.ToString();
                    model.start = "比赛开始时间：" + dt.Year + "-" + dt.Month + "-" + dt.Day + " " + dt.Hour + ":" + minute;
                    dt = DateTime.Parse(item["end"].ToString());
                    minute = dt.Minute.ToString().Length == 1 ? "0" + dt.Minute : dt.Minute.ToString();
                    model.end = "比赛结束时间：" + dt.Year + "-" + dt.Month + "-" + dt.Day + " " + dt.Hour + ":" + minute;
                    int type = Convert.ToInt32(item["type"].ToString());
                    model.type = Convert.ToInt32(item["type"].ToString());//1：已参加 2：未参加 3:已结束
                    model.coin = Convert.ToInt32(item["coin"].ToString());
                    model.first_coin = Convert.ToInt32(item["firstcoin"].ToString());
                    model.cost = item["cost"].ToString();
                    model.number = "目前人数：" + item["number"].ToString();
                    model.grades = "我的成绩：" + item["grades"].ToString();
                    model.pos = "我的名次：" + item["pos"].ToString();
                    model.reward = item["reward"].ToString();
                    model.prompt = item["prompt"].ToString();
                    model.show = item["show"].ToString();
                    model.props = item["IsUseStage"] == null ? false : Convert.ToBoolean(item["IsUseStage"].ToString());

                    response.match.Add(model);
                }
            }
            return response;
        }

        public MatchRankResponse GetMatchRankList(MatchRankRequset request)
        {
            MatchRankResponse response = new MatchRankResponse()
            {
                rank = new List<MatchRankManage>()
            };

            string userAccount = BaseController.Account(request.info);

            if (string.IsNullOrEmpty(userAccount))
                return response;
            TaskDAL dal = new TaskDAL();
            DataSet ds = dal.GetMatchRank(userAccount, request.id);

            foreach (DataRow item in ds.Tables[0].Rows)
            {
                MatchRankManage model = new MatchRankManage()
                {
                    num = item["num"].ToString(),
                    name = item["name"].ToString(),
                    fraction = item["fraction"].ToString(),
                    myself = item["isMyself"].ToString() == "True" ? 1 : 0
                };
                response.rank.Add(model);
            }

            return response;
        }

    }
}
